import React from "react";
import ReactDom from "react-dom";
import {Link} from "react-router-dom";
import createHistory from 'history/createHashHistory'

import {Col,Row,Menu,Icon,Form,Input,Button,message } from "antd";
import AdminHeader from './header.jsx';
import {REQUEST,STORAGE} from "../../util/comm.jsx";
import {userUrl} from '../../util/constant.js';
const _request=new REQUEST();
const _storage=new STORAGE();

class AdminLogin extends React.Component{
	constructor(){
		super();
		this.state={
			isLogining:false,
			errorMessage:'',
		}
	}
	componentDidMount(){
	}
	doLogin(e){
		e.preventDefault();
		this.setState({
			isLogining:true,
			errorMessage:''
		})
		let data=['username','password'];
		let param=this.props.form.getFieldsValue(data);
		this.props.form.validateFields(data,(error,value)=>{
			if(!error){
				param.type="admin";
				_request.post(userUrl,param).then(res=>res.json()).then(res=>{

					if(res.code==1){
						_storage.setStorage('ReactNews_AdminUserInfo',res.data);
						new createHistory().push('/');
					}
					else{
						this.setState({
							errorMessage:res.message,
						})
					}
				}).catch(e=>{
					this.setState({
						errorMessage:res.message,
					})
				})
			}
		});
	}
	render(){
		const FormItem = Form.Item;
		const { getFieldDecorator,getFieldValue } = this.props.form;
		return(
			<div className="admin-login">
				<input type="text" style={{height:0,border:'none'}}/>
				<input type="password" style={{height:0,border:'none'}}/>
				<div className="user-box">
					<div className="user-tip">请登录</div>
					<div className="user-form">
						<Form layout="vertical" onSubmit={this.doLogin.bind(this)}>
							<FormItem >
								 {getFieldDecorator('username', {
								     rules: [{ required: true, message: 'Please input your username!' }],
								  })(
								      <Input prefix={<Icon type="user" style={{ fontSize: 15 }} />} placeholder="请输入用户名" autoComplete={false}  />
								  )}
							</FormItem>
							<FormItem >
								 {getFieldDecorator('password', {
								     rules: [{ required: true, message: 'Please input your username!' }],
								  })(
								      <Input prefix={<Icon type="lock" style={{ fontSize: 15 }} />} type='passWord' placeholder="请输入你的密码" autoComplete={false} />
								  )}
							</FormItem>
							<div className="loading" style={this.state.isLogining?{visibility:'visible'}:{visibility:'hidden'}}>{this.state.errorMessage?this.state.errorMessage:"登陆中..."}</div>
							<FormItem>
								<Button  type="primary" htmlType="submit">登录</Button>
							</FormItem>
						</Form>
					</div>
				</div>
			</div>
		)
	}
}
AdminLogin=Form.create({})(AdminLogin);
export default AdminLogin;


